<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ total }}条记录</span>
        <el-button
          style="float: right"
          round
          icon="el-icon-plus"
          size="small"
          type="primary"
          @click="add"
          >操作按钮</el-button
        >
      </div>
      <div>
        <el-table :data="atricle" stripe border>
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="标题" prop="stem"></el-table-column>
          <el-table-column label="作者" prop="creator"></el-table-column>
          <el-table-column label="点赞" prop="likeCount"></el-table-column>
          <el-table-column label="浏览数" prop="views"></el-table-column>
          <el-table-column label="更新时间" prop="createdAt"></el-table-column>
          <el-table-column label="操作">
            <template #default="scoped">
              <div class="zxc">
                <el-button
                  type="text"
                  size="medium"
                  icon="el-icon-view"
                  @click="prvView(scoped.row.id)"
                ></el-button>
                <el-button
                  type="text"
                  size="medium"
                  icon="el-icon-edit"
                  @click="showEdit(scoped.row.id)"
                ></el-button>
                <el-button
                  type="text"
                  size="medium"
                  @click="del(scoped.row.id)"
                  icon="el-icon-delete"
                ></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="params.current"
        :page-size="params.pagSize"
        @current-change="changeCurrent"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      size="50%"
      :title="title"
      :visible.sync="drawer"
      direction="rtl"
      @close="closeFn"
    >
      <el-form
        v-if="title !== '预览面经'"
        label-width="70px"
        :model="form"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            prop="content"
            @blur="$refs.formRef.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveAdd">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <div v-else>
        <h4>{{ form.stem }}</h4>
        <el-divider></el-divider>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  addArticle,
  delArticle,
  getArticleList,
  getArticleDetail,
  updateArticle
} from '@/api/article'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  props: {},
  data() {
    return {
      drawer: false,
      params: {
        current: 1,
        pageSize: 10
      },
      atricle: [],
      total: null,
      title: '',
      form: {
        stem: '', // 标题
        content: '' // 内容
      },
      rules: {
        stem: [{ required: true, message: '内容不能为空', trggier: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trggier: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getArticleList()
  },
  methods: {
    async getArticleList() {
      const res = await getArticleList(this.params)
      this.atricle = res.data.rows
      this.total = res.data.total
    },
    changeCurrent(val) {
      this.params.current = val
      this.getArticleList()
    },
    async del(id) {
      // await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.$message({
      //       type: 'success',
      //       message: '删除成功!'
      //     })
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消删除'
      //     })
      //   })
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delArticle(id)
        this.$message.success('删除成功')
        this.getArticleList()
      } catch (error) {
        this.$message('取消删除')
      }
    },
    add() {
      this.form = {
        stem: '',
        content: ''
      }
      this.drawer = true
      this.title = '添加面经'
    },
    closeFn() {
      if (this.title !== '预览面经') {
        this.$refs.formRef.resetFields()
      }
    },
    async saveAdd() {
      try {
        await this.$refs.formRef.validate()
        if (this.title === '修改面经') {
          const form = {}
          form.stem = this.form.stem
          form.id = this.form.id
          form.content = this.form.content
          await updateArticle(form)
          this.$message.success('修改成功')
        } else {
          await addArticle(this.form)
          this.$message.success('添加成功')
        }
        this.getArticleList()
        this.drawer = false
      } catch (error) {
        console.log(error)
      }
    },
    async showEdit(id) {
      this.title = '修改面经'
      const res = await getArticleDetail(id)
      this.form = res.data
      this.drawer = true
      console.log(this.form)
    },
    async prvView(id) {
      this.title = '预览面经'
      this.drawer = true
      const res = await getArticleDetail(id)
      this.form.stem = res.data.stem
      this.form.content = res.data.content
    }
  }
}
</script>

<style scoped>
.box-card {
  margin-top: 15px;
  box-shadow: 0 1px 22px 0 rgba(0 0 0 / 10%);
}
.zxc {
  display: flex;
  justify-content: space-around;
}
.el-pagination {
  text-align: center;
  margin-top: 16px;
}
::v-deep .ql-editor {
  min-height: 200px;
}
</style>
